const net = require("net");

const server = net.createServer();
const clientlist = [];

//处理客户端连接
server.on("connection", (client) => {
  console.log("come a client");
  clientlist.push(client);

  //处理客户端的消息
  client.on("data", (data) => {
    console.log("transmit a msg:", data.toString());
    clientlist.forEach((element) => {
      if (element != client) {
        element.write(data);
      }
    });
  });

  client.on("error", (error) => {
    console.log(error);
    clientlist.pop(client);
  });
});

//监听
console.log("start listen 9090");
server.listen(9090);
